package com.quectel.core.module.village.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.quectel.constant.global.SqlConstants;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.village.dao.VillageRoomDao;
import com.quectel.core.module.village.dto.VillageFloorDto;
import com.quectel.core.module.village.dto.VillageRoomDto;
import com.quectel.core.module.village.dto.datav.RoomCountDto;
import com.quectel.core.module.village.entity.VillageRoomEntity;
import com.quectel.core.module.village.service.VillageFloorService;
import com.quectel.core.module.village.service.VillageRoomService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-10-18 14:41:47
 */
@DubboService
public class VillageRoomServiceImpl extends ServiceImpl<VillageRoomDao, VillageRoomEntity> implements VillageRoomService {

    private static final Logger LOGGER = LoggerFactory.getLogger(VillageRoomServiceImpl.class);
    private static final String VILLAGE_ROOM_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "VILLAGE_ROOM_CACHE_BY_ID:";
    @Autowired
    private VillageRoomDao villageRoomDao;
    @Autowired
    private VillageFloorService villageFloorService;


    @Override
    public RoomCountDto countRoom(Map<String, Object> params) {
        return villageRoomDao.countRoom(params);
    }

    @Override
    public VillageRoomDto selectByName(Long floorId, String name) {
        VillageRoomEntity entity = villageRoomDao.selectOne(new LambdaQueryWrapper<VillageRoomEntity>()
                .eq(VillageRoomEntity::getFloorId, floorId)
                .eq(VillageRoomEntity::getName, name)
                .last(SqlConstants.MYSQL_LIMIT_ONE)
        );
        return paddingField(CopyUtils.copyObj(entity, VillageRoomDto.class));
    }

    @Override
    public void updateBatch(List<VillageRoomDto> updateRooms) {
        List<VillageRoomEntity> villageRoomEntities = CopyUtils.copyList(updateRooms, VillageRoomEntity.class);
        super.updateBatchById(villageRoomEntities);
    }

    @Override
    public void saveBatch(List<VillageRoomDto> list) {
        List<VillageRoomEntity> villageRoomEntities = CopyUtils.copyList(list, VillageRoomEntity.class);
        for (VillageRoomEntity villageRoomEntity : villageRoomEntities) {
            villageRoomEntity.setId(Snowflake.nextId());
        }
        super.saveBatch(villageRoomEntities);
    }

    @Override
    public VillageRoomDto selectByNameWithBuildingId(Long buildingId, String name) {
        VillageRoomEntity entity = villageRoomDao.selectOne(new LambdaQueryWrapper<VillageRoomEntity>()
                .eq(VillageRoomEntity::getBuildingId, buildingId)
                .eq(VillageRoomEntity::getName, name)
                .last(SqlConstants.MYSQL_LIMIT_ONE)
        );
        return paddingField(CopyUtils.copyObj(entity, VillageRoomDto.class));
    }

    @Override
    public List<VillageRoomDto> selectByBuildingId(Long buildingId) {
        List<VillageRoomEntity> entityList = villageRoomDao.selectList(new LambdaQueryWrapper<VillageRoomEntity>()
                .eq(VillageRoomEntity::getBuildingId, buildingId)
        );
        List<VillageRoomDto> list = CopyUtils.copyList(entityList, VillageRoomDto.class);
        for (VillageRoomDto villageRoomDto : list) {
            paddingField(villageRoomDto);
        }
        return list;
    }

    @Override
    public List<VillageRoomDto> selectByFloorId(Long floorId) {
        List<VillageRoomEntity> entityList = villageRoomDao.selectList(new LambdaQueryWrapper<VillageRoomEntity>()
                .eq(VillageRoomEntity::getFloorId, floorId)
        );
        List<VillageRoomDto> list = CopyUtils.copyList(entityList, VillageRoomDto.class);
        for (VillageRoomDto villageRoomDto : list) {
            paddingField(villageRoomDto);
        }
        return list;
    }

    @Override
    public VillageRoomDto selectById(Long id) {
        VillageRoomEntity entity = villageRoomDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, VillageRoomDto.class));
    }

    @Override
    public VillageRoomDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                VILLAGE_ROOM_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public List<VillageRoomDto> queryList(Map<String, Object> params) {
        List<VillageRoomEntity> list = villageRoomDao.queryList(params);
        List<VillageRoomDto> result = CopyUtils.copyList(list, VillageRoomDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private VillageRoomDto paddingField(VillageRoomDto dto) {
        if (dto == null) {
            return null;
        }
        if (dto.getFloorId() != null) {
            VillageFloorDto villageFloorDto = villageFloorService.selectCacheById(dto.getFloorId());
            if (villageFloorDto != null) {
                dto.setVillageName(villageFloorDto.getVillageName());
                dto.setBuildingName(villageFloorDto.getBuildingName());
                dto.setFloorName(villageFloorDto.getName());
                dto.setFloorNo(villageFloorDto.getFloorNo());
            }
        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return villageRoomDao.queryTotal(params);
    }

    @Override
    public Long save(VillageRoomDto dto) {
        VillageRoomEntity entity = CopyUtils.copyObj(dto, VillageRoomEntity.class);
        entity.setId(Snowflake.nextId());
        villageRoomDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(VillageRoomDto dto) {
        VillageRoomEntity entity = CopyUtils.copyObj(dto, VillageRoomEntity.class);
        villageRoomDao.updateById(entity);
        CacheKit.invalidRedisCache(VILLAGE_ROOM_CACHE_BY_ID + dto.getId());
    }


    @Override
    public void deleteById(Long id) {
        villageRoomDao.deleteById(id);
        CacheKit.invalidRedisCache(VILLAGE_ROOM_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }

}
